<?php
/**
*  +----------------------------------------------------------------------------------------------+
*   | Explain:
*  +----------------------------------------------------------------------------------------------+
*   | Author: chenyq
*	+----------------------------------------------------------------------------------------------+
*   | Creater Time : 2014-03-05 	
*  +----------------------------------------------------------------------------------------------+
**/

	class RepeatuserAction extends BaseAction {
		protected $model;
		function _initialize(){
			parent::_initialize();
			$this->model = M('UserLog');
		}
		
		/**
		 * (non-PHPdoc)
		 * @see BaseAction::index()
		 * 重复帐号列表
		 */
		function index(){
			$count = M()->query( "select u1.id,u2.id,u1.username,u1.mobile,u1.email,u2.username,u2.mobile,u2.email,(
					select COUNT(ua.id) from user_assets ua where ua.userid=u1.id) conta,(
					select COUNT(um.id) from user_money um where um.userid=u1.id) contm 
					from `user` u1,`user` u2 where u1.id != u2.id and u1.username =u2.username and u1.username !='' HAVING conta = 0 and contm = 0 
					ORDER BY u1.id DESC");
			$cot = count($count);
			$this->assign ( "totalCount",$cot);
			if ($count > 0) {
				$page = new Page ( $cot, $this->listrows );
				$list = M()->query( "select u1.id id1,u2.id id2,u1.username username1,u1.mobile mobile1,u1.email email1,u2.username username2,u2.mobile mobile2,u2.email email2,(
					select COUNT(ua.id) from user_assets ua where ua.userid=u1.id) conta,(
					select COUNT(um.id) from user_money um where um.userid=u1.id) contm 
					from `user` u1,`user` u2 where u1.id != u2.id and u1.username =u2.username and u1.username !='' HAVING conta = 0 and contm = 0 
					ORDER BY u1.id DESC limit {$page->firstRow},{$page->listRows}" );
			}
			$this->assign('list',$list);
			$this->display();
		}
		/**
		 * 更新重复帐号用户
		 */
		function updateusername(){
			$list = M()->query( "select u1.id id1,u2.id id2,u1.username username1,u1.mobile mobile1,u1.email email1,u2.username username2,u2.mobile mobile2,u2.email email2,(
					select COUNT(ua.id) from user_assets ua where ua.userid=u1.id) conta,(
					select COUNT(um.id) from user_money um where um.userid=u1.id) contm
					from `user` u1,`user` u2 where u1.id != u2.id and u1.username =u2.username and u1.username !='' HAVING conta = 0 and contm = 0
					ORDER BY u1.id DESC");			
			$i=1;
			foreach ( $list as $us ) {
				
				$count=M('User')->where(array('username'=>$us['username1']))->select();
// 				dump($count);
// 				exit();
				$data = array();
				if(count($count) > 1){
// 						$user=M('User')->where(array('id'=>$us['id1']))->find();
// 						$user['username'] = 'b'.$user['username'];
// 						$user['mobile'] = '';
// 						$user['email'] = '';
						
						$data['id']=$us['id1'];
						$data['username'] = 'a'.$us['username1'].$i;
						$data['mobile'] = '';
						$data['email'] = '';
						$res=M('User')->save($data);
						if($res===false){
							$this->_ajm('300','操作失败！',1);
						}
						$i++;
				}
			}
// 			dump($user);
// 			exit();
			$this->_ajm('200','操作成功！',1);
		}
		/**
		 * 更新重复邮箱用户
		 */
		function updateuseremail(){
			$list = M()->query( "select u1.id id1,u2.id id2,u1.username username1,u1.mobile mobile1,u1.email email1,u2.username username2,u2.mobile mobile2,u2.email email2,(
					select COUNT(ua.id) from user_assets ua where ua.userid=u1.id) conta,(
					select COUNT(um.id) from user_money um where um.userid=u1.id) contm
					from `user` u1,`user` u2 where u1.id != u2.id and u1.email =u2.email and u1.email != '' HAVING conta = 0 and contm = 0
					ORDER BY u1.id DESC");
			$i=1;
			foreach ( $list as $us ) {
				$count=M('User')->where(array('email'=>$us['email1']))->select();
				$data = array();
				if(count($count) > 1){
					$data['id']=$us['id1'];
					$data['username'] = 'b'.$us['username1'].$i;
					$data['mobile'] = '';
					$data['email'] = '';
					$res=M('User')->save($data);
					if($res===false){
						$this->_ajm('300','操作失败！',1);
					}
					$i++;
				}
			}
			$this->_ajm('200','操作成功！',1);
		}
		/**
		 * 更新重复手机用户
		 */
		function updateuseremobile(){
			$list = M()->query( "select u1.id id1,u2.id id2,u1.username username1,u1.mobile mobile1,u1.email email1,u2.username username2,u2.mobile mobile2,u2.email email2,(
					select COUNT(ua.id) from user_assets ua where ua.userid=u1.id) conta,(
					select COUNT(um.id) from user_money um where um.userid=u1.id) contm
					from `user` u1,`user` u2 where u1.id != u2.id and u1.mobile =u2.mobile and u1.mobile != '' HAVING conta = 0 and contm = 0
					ORDER BY u1.id DESC");
			$i=1;
			foreach ( $list as $us ) {
				$count=M('User')->where(array('mobile'=>$us['mobile1']))->select();
				$data = array();
				if(count($count) > 1){
					$data['id']=$us['id1'];
					$data['username'] = 'c'.$us['username1'].$i;
					$data['mobile'] = '';
					$data['email'] = '';
					$res=M('User')->save($data);
					if($res===false){
						$this->_ajm('300','操作失败！',1);
					}
					$i++;
				}
			}
			$this->_ajm('200','操作成功！',1);
		}
	}